/*
 * Copyright (C) 2013 Michael
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.jm.jmsql.sql;

import com.jm.jmsql.models.ResultSetTableModel;
import com.jm.jmsql.xport.XPortManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

/**
 *
 * @created Feb 20, 2013
 * @author Michael L.R. Marques
 */
public class SQLEditorTablePopupMenu extends JPopupMenu {
    
    /**
     * 
     * @param table
     * @param x
     * @param y 
     */
    public static void show(final JTable table, int x, int y) {
        SQLEditorTablePopupMenu popup = new SQLEditorTablePopupMenu();
        if (table.getModel() instanceof ResultSetTableModel) {
            final ResultSetTableModel model = (ResultSetTableModel) table.getModel();
            if (model.getTotalRows() > 0) {
                // XPort Results
                JMenuItem export = new JMenuItem("XPort Results...");
                export.setIcon(new ImageIcon(popup.getClass().getClassLoader().getResource("com/jm/jmsql/icons/xport.png")));
                export.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        final XPortManager xpm = new XPortManager(table.getRootPane());
                        xpm.setResultSet(model.getResults());
                        java.awt.EventQueue.invokeLater(new Runnable() {
                            @Override public void run() {
                                xpm.setVisible(true);
                            }
                        });
                    }
                });
                // Add Popup menu items
                popup.add(export);
            }
        }
        
        if (popup.getComponentCount() > 0) {
            popup.show(table.getParent(), x, y);
        }
    }
    
}
